package com.lcz.musicplay.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import com.lcz.musicplay.config.Constants;
import com.lcz.musicplay.domain.Music;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2016/8/19.
 */
public class MediaUtils {
    //歌曲列表
    public static List<Music> musicList = new ArrayList<>();
    //播放状态
    public static int CURSTATE = Constants.STATE_STOP;
    //当前进度
    public static int CURPOSION = 0;
    public static int CURMODLE = Constants.MODEL_NORMAL;

    /**
     * 加载本地音乐
     *
     * @param context
     */
    public static void initMusicList(Context context) {
        //清空列表
        musicList.clear();
        if (isSdcardExit()) {
            //uri
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            //查找元素：歌名、歌手、路径
            String[] projection = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA};
            //根据内容提供者查找音乐
            Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
            while (cursor.moveToNext()) {
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                System.out.println("path===" + path);
                Music music = new Music(title, artist, path);
                musicList.add(music);
            }

        } else {
            System.out.println("SD卡不存在~");
        }

    }


    public static boolean isSdcardExit() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 当前进度时长
     * 返回格式为    xx:xx 的字符串
     *
     * @param num 毫秒值
     * @return
     */
    public static String int2Str(int num) {
        String result = "";
        int ss = num / 1000;
        //秒
        int sec = ss % 60;
        //分
        int min = ss / 60;
        /*if (min >= 10) {
            result = min + ":" + (sec >= 10 ? sec : "0" + sec);
        } else {
            result = "0" + min + ":" + (sec >= 10 ? sec : "0" + sec);
        }*/
        result = (min >= 10 ? min : "0" + min) + ":" + (sec >= 10 ? sec : "0" + sec);
        return result;
    }

    public static File getLrcFile(String title, String artist) {

        File file = null;
        // 在目录"MIUI/music/lyric/"下查找相应的歌词 /storage_int/0/MIUI/music/lyric/Rain_秦基博.lrc
        String sdCardPath = Environment.getExternalStorageDirectory() + "/MIUI/music/lyric/";
        if (artist != null && !artist.isEmpty()) {
            String lrcName = title + "_" + artist + ".lrc";
            System.out.println("歌词路径：" + sdCardPath + lrcName);
            file = new File(sdCardPath + lrcName);
            if (!file.exists()) {
                lrcName = title + "_" + artist + ".txt";
                file = new File(lrcName);
                if (!file.exists()) {
                    return null;
                }
            }

        } else {
            String lrcName = title + ".lrc";
            System.out.println("歌词路径：" + sdCardPath + lrcName);
            file = new File(sdCardPath + lrcName);
            if (!file.exists()) {
                lrcName = title + ".txt";
                file = new File(lrcName);
                if (!file.exists()) {
                    return null;
                }
            }
        }
        return file;
    }
}
